import { Component, Injector, ViewEncapsulation, OnInit } from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineListDto,
  MaterialOutListDto,
  RequirementMaterialListDto,
  MaterialOutServiceProxy,
  ToolingInfoDto,
} from "@shared/service-proxies/service-proxies";
import { PipelineNodeServiceProxy } from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";
import { appModuleAnimation } from "@shared/animations/routerTransition";

@Component({
  selector: "zl1",
  templateUrl: "./zl1.component.html",
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class ZL1ModalComponent extends AppComponentBase implements OnInit {
  saving = false;
  isLoading = false;

  pipe: PipelineListDto = new PipelineListDto();

  bills = new Array<RequirementMaterialListDto>();
  subTaskCode = "";
  drawingCode = "";

  outs = new Array<MaterialOutListDto>();

  toolings = new Array<ToolingInfoDto>();

  //table cols
  cols: any[];
  selectedColumns: any[];

  zhiKongKaNo = "";

  constructor(
    injector: Injector,
    private _outMaterialServiceProxy: MaterialOutServiceProxy,
    private _taskServiceProxy: PipelineNodeServiceProxy
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.cols = [
      {
        field: "taskCode",
        header: "任务号",
        width: "150px",
        isDefaultSelect: true,
      },
      {
        field: "drawingCode",
        header: "图号",
        width: "120px",
        isDefaultSelect: true,
      },
      {
        field: "materialName",
        header: "名称",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "invFurBatch",
        header: "炉批号",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "invCode",
        header: "物资编码",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "hgz_zjd",
        header: "质检编号",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "paiHao",
        header: "牌号",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "hgzNo",
        header: "合格证",
        width: "140px",
        isDefaultSelect: true,
      },
      {
        field: "hgz_zyd",
        header: "质疑单号",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "materialStatus",
        header: "材料状态",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "materialSpec",
        header: "规格",
        width: "120px",
        isDefaultSelect: true,
      },
      {
        field: "techCondition",
        header: "技术状态",
        width: "140px",
        isDefaultSelect: true,
      },
      {
        field: "zhuanYongInfo",
        header: "专料专用信息",
        width: "100px",
        isDefaultSelect: true,
      },
    ];

    this.selectedColumns = this.cols.filter((el) => {
      return el.isDefaultSelect;
    });
  }

  load(pipe: PipelineListDto): void {
    this.pipe = pipe;

    this._taskServiceProxy.getZhiKongKaNo(pipe.id).subscribe((result) => {
      this.zhiKongKaNo = typeof result === "object" ? "" : result;
    });
    this._taskServiceProxy
      .getRequirementBillByTask(pipe.id)
      .subscribe((result) => {
        this.bills = result.requirementMaterials;
        this.subTaskCode = result.subTaskCode;
        this.drawingCode = result.drawingCode;
      });

    this.getTechDocInfoTooling();

    this.getOuts();
  }

  getOuts() {
    this._outMaterialServiceProxy
      .getOutWarehouseBills(this.pipe.id)
      .subscribe((result) => {
        this.outs = result.items;
      });
  }

  save(): void {
    this.saving = true;

    this._taskServiceProxy
      .setZhiKongKaNo(this.pipe.id, this.zhiKongKaNo)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe((result) => {
        this.notify.success("记录质控卡成功");
      });
  }

  getTechDocInfoTooling() {
    this.isLoading = true;
    this._taskServiceProxy
      .getTechDocToolingInfo(this.pipe.id)
      .pipe(
        finalize(() => {
          this.isLoading = false;
        })
      )
      .subscribe((result) => {
        this.toolings = result.items;
      });
  }
}
